;(function() {
	var firstVisitTime = null;
	var previousVisitTime = null;
	var currentvisitTime = null;
	var visitsCount = null;
	var visitorId = null;
	
	var widgetName = null;
	var uniqueId = null;
	var hostname = null;
	var page_encoding = null;
	
	//encoding and hostname are optional parameters - used in pageview tracking
	GoogleAnalytics = function(_widgetName, _uniqueId, _hostname, _encoding)
	{
		widgetName = _widgetName;
		uniqueId = _uniqueId;
		
		if(_hostname == undefined || _hostname == null)
		{
			hostname = '127.0.0.1'; //assume default is localhost
		}
		else
		{
			hostname = _hostname;
		}

		if(_encoding == undefined || _encoding == null)
		{
			page_encoding = 'UTF-8'; //assume default is UTF-8
		}
		else
		{
			page_encoding = _encoding;
		}
		
		initialize();
	}
	GoogleAnalytics.prototype.trackEvent = function(category, action, label)
	{
		if(label == undefined || label == null)
		{
			label = '';
		}
		
		httpReq(
			getTrackURL(category, action, label), 
			trackEventHandler
		);
	}
	GoogleAnalytics.prototype.trackPageview = function(a_URLToTrack)
	{
		if(a_URLToTrack == undefined || a_URLToTrack == null)
		{
			a_URLToTrack = '/'; //assume default is root
		}

		httpReq(
			getTrackURLPageView(a_URLToTrack), 
			trackPageviewHandler
		);
	}
	
	function saveData()
	{
		var toSave = firstVisitTime + "|" + 
			currentVisitTime + "|" + 
			visitorId + "|" + 
			visitsCount;
			
		widget.setPreferenceForKey(toSave, 'UserTracking');
	}
	function initialize()
	{
		var savedData = widget.preferenceForKey('UserTracking');
		
		var now = Math.floor(new Date().getTime() / 1000);
	
		currentVisitTime = now;
		
		if(savedData)
		{
			var splitted = savedData.split('|');
			
			firstVisitTime = splitted[0];
			previousVisitTime = splitted[1];
			
			visitorId = splitted[2];
			
			visitsCount = parseInt(splitted[3]) + 1;
		}
		else
		{
			firstVisitTime = now;
			
			previousVisitTime = now;
			
			visitorId = Math.floor(Math.random() * 1000000000);
			
			visitsCount = 1;
		}
		saveData();
	}
	function trackEventHandler(req, success)
	{
		alert(1);
	}
	function trackPageviewHandler(req, success)
	{
		alert(2);
	}
	function getTrackURL(category, action, label)
	{
		return "http://www.google-analytics.com/__utm.gif?" +
			"utmwv=4.6.5&" +
			"utmn=" + Math.floor(Math.random() * 100000000) + "&" +
			"utmhn=127.0.0.1&" +
			"utmt=event&" +
			"utme=5(" + encodeURIComponent(category) + 
				"*" + encodeURIComponent(action) + 
				"*" + encodeURIComponent(label) + ")&" +
			"utmcn=1&" +
			"utmdt=" + encodeURIComponent(widgetName) + "&" + 
			"utmhid=" + Math.floor(Math.random() * 100000000) + "&" +
			"utmr=0&" +
			"utmp=%2Findex.html&" +
			"utmac=" + uniqueId + "&" + 
			"utmcc=__utma%3D1." + visitorId + 
			"." + firstVisitTime + 
			"." + previousVisitTime + "." + currentVisitTime + 
			"." + visitsCount + 
			"%3B%2B__utmz%3D1." + firstVisitTime + 
			"." + visitsCount + 
			".1.utmcsr%3D(direct)|utmccn%3D(direct)|utmcmd%3D(none)%3B";
	}
	function getTrackURLPageView(a_URLToTrack)
	{
		return "http://www.google-analytics.com/__utm.gif?" +
			"utmwv=4.8.6" +
			"utmn=" + Math.floor(Math.random() * 100000000) + "&" +
			"utmhn=" + hostname + "&"
			"utmcs=" + page_encoding +
			"utmdt=" + encodeURIComponent(widgetName) + "&" + 
			"utmhid=" + Math.floor(Math.random() * 100000000) + "&" +
			"utmr=-&" +
			"utmp=" + encodeURIComponent(a_URLToTrack) + "&" +
			"utmac=" + uniqueId + "&" + 
			"utmcc=__utma%3D1." + visitorId + 
			"." + firstVisitTime + 
			"." + previousVisitTime + "." + currentVisitTime + 
			"." + visitsCount + 
			"%3B%2B__utmz%3D1." + firstVisitTime + 
			"." + visitsCount + 
			".1.utmcsr%3D(direct)|utmccn%3D(direct)|utmcmd%3D(none)%3B&utmu=D";
	}
	function httpReq(url, func)
	{
		try
		{
			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		}
		catch(e)
		{
		}
	
		var request = new XMLHttpRequest();
		
		request.onreadystatechange = function()
		{
			if(request.readyState == 4)
			{
				if(request.status == 200)
					func(request, true);
				else
					func(request, false);
			}
		}
		request.open( "GET", url, true );
	
		request.send(null);
	}
})();